package com.linsir.lin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.plugins.pagination.PageDto;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.linsir.lin.dto.GridDTO;
import com.linsir.lin.dto.MemberDTO;
import com.linsir.lin.dto.OrganizationDTO;
import com.linsir.lin.mapper.MemberMapper;
import com.linsir.lin.pojo.Member;
import com.linsir.lin.pojo.Organization;
import com.linsir.lin.service.MemberOrganService;
import com.linsir.lin.service.MemberService;
import com.linsir.lin.service.OrganizationService;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicReference;

/**
 * @author ：linsir
 * @date ：Created in 2021/7/9 10:13
 * @description：接口实现
 * @modified By：
 * @version: 1.0.0
 */

@Service
public class MemberServiceImpl extends ServiceImpl<MemberMapper, Member> implements IService<Member>, MemberService {

    @Autowired
    private MemberOrganService memberOrganService;

    @Autowired
    private OrganizationService organizationService;

    @Override
    public GridDTO<MemberDTO> grid(OrganizationDTO organizationDTO) throws InvocationTargetException, IllegalAccessException {

        List<OrganizationDTO> organizationDTOList  =organizationService.getTree(organizationDTO);
        List<Integer> memberIds = new  ArrayList<Integer> ();
        organizationDTOList.forEach(o->{
            List<Integer> mids = memberOrganService.getMIdsByOId(o.getId());
            if (mids!=null&&mids.size()>0)
            {
                memberIds.addAll(mids);
            }
        });
        List<Member> members =  new ArrayList<Member>();
        List<MemberDTO> memberDTOS = new ArrayList<MemberDTO>();
        if (memberIds!=null&&memberIds.size()>0)
        {
            members=listByIds(memberIds);
            for (Member member : members)
            {
                MemberDTO memberDTO= toDTO(member);
                memberDTOS.add(memberDTO);
            }
        }
        Long size = new Long(memberDTOS.size());
        return new GridDTO<MemberDTO>("0","列表",size,memberDTOS);
    }


    public GridDTO<MemberDTO> grid()
    {
        Page<Member> pageDto = new PageDto<Member>();
        QueryWrapper<Member> wrapper=new QueryWrapper<Member>();
        pageDto = baseMapper.selectPage(pageDto,wrapper);
        List<Member> members = pageDto.getRecords();
        List<MemberDTO> memberDTOS = new ArrayList<MemberDTO>();
        members.forEach(member -> {
            try {
                MemberDTO memberDTO= toDTO(member);
                memberDTOS.add(memberDTO);
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }

        });
        return new GridDTO<MemberDTO>("0","列表",pageDto.getSize(),memberDTOS);
    }

    @Override
    public GridDTO<MemberDTO> grid(Integer organId) throws InvocationTargetException, IllegalAccessException {
        OrganizationDTO organizationDTO =organizationService.getByOrganId(organId);
        return grid(organizationDTO);
    }

    @Override
    public MemberDTO toDTO(Member member) throws InvocationTargetException, IllegalAccessException {
        MemberDTO memberDTO =new MemberDTO();
        BeanUtils.copyProperties(memberDTO,member);

        List<Organization> organizations=organizationService.getOrganizationsByIds(memberOrganService.getOIdsByMId(memberDTO.getId()));
        memberDTO.setOrganizations(organizations);
        AtomicReference<String> strs = new AtomicReference<>("");
                organizations.forEach( o ->{
                    strs.set(strs +","+o.getName());
                });
                memberDTO.setOrganStrs(strs.get().substring(0,strs.get().length()-1));
        return memberDTO;
    }

    @Override
    public List<Organization> getOrganizationsByMember(Member member) {
        return organizationService.getOrganizationsByIds(memberOrganService.getOIdsByMId(member.getId()));
    }


}
